package com.xxcloud.service.note.impl;

import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import com.google.common.base.Preconditions;
import com.xxcloud.dao.dto.xxcloud.Folder;
import com.xxcloud.dao.dto.xxcloud.Note;
import com.xxcloud.model.exception.note.NoteException;
import com.xxcloud.model.web.note.dto.NoteDTO;
import com.xxcloud.service.folder.IFolderService;

/**
 * Created by chenbingwen on 19/10/29.
 */
public class AbstractNoteService {

    @Resource(name = "folderService")
    private IFolderService folderService;

    protected Note convert2Note(NoteDTO dto) {
        Note note = new Note();
        if (dto != null) {
            BeanUtils.copyProperties(dto, note);
        }
        return note;
    }

    protected void checkNoteDTO(NoteDTO dto) throws NoteException {
        Preconditions.checkNotNull(dto);
        Long operatorId = dto.getOperatorId();
        Preconditions.checkArgument(operatorId != null && operatorId > 0);
        Long folderId = dto.getFolderId();
        if (folderId == null || folderId <= 0) {
            throw new NoteException(NoteException.ILLEGAL_OPERATION);
        }
        Folder folder = folderService.getFolderById(folderId);
        if (folder == null) {
            throw new NoteException(NoteException.NOTE_FOLDER_NOT_EXISTS);
        }
    }
}
